function Master_Policy_Decomp_EEGap_a_stand_mar_v4_Heter_co2_m_adj_pos(subsample,inc,set_seed,matlab_seed,nb_draws,d_ext_carbon_price)


% Created: 24/10/2024
% This code look for an attribute-based standard that achieve the same reduction in the externality cost
% than the tax based on the marginal external cost for carbon: d_ext_carbon_price

% In this code, all models that do not meet the more stringent standard are
% marginal, i.e., no models will exit the market as results of a more
% stringent standard, no matter what the adjustment is.

% Dependencies:
% Fox model v1 estimated for all income groups
% Master_Fox_mixed_discrete_FEdemoshrt_3param_grID_wtp_j_bs(44000,16,1,1,50, j, index_bs)
												

% Parameters for the policy simulation

  elec_scale=1;  % Ensure that we keep heterogeneous electricity price
  p_elec=0;      % Ensure that we keep heterogeneous electricity price
% p_elec=0.11;   % Homogeneous electricity price
% kwh;
% elec;
% estar;
% d_ext=0.02;            % Homogeneous carbon externality $/kEh
% d_ext_carbon_price=50; % Heterogeneous carbon externality $/ton CO2
  r=0.05;                % Market discount rate
  lifetime=18;           % Expected lifetime of the appliance
  MCPF=1;

  
  rho_5=1/(1+r);
  LLC_5_18=rho_5*(1-rho_5^18)/(1-rho_5);
  
  rho_2=1/(1+0.02);
  LLC_2_18=rho_2*(1-rho_2^18)/(1-rho_2);
  
  rho_12=1/(1+0.12);
  LLC_12_18=rho_12*(1-rho_12^18)/(1-rho_12);
 
  rebate_estar_denom_scale=0;

  Nsample=3500;

tic

%Create Diary
date_now = clock;
date_now = strcat(num2str(date_now(1)),'_',num2str(date_now(2)),'_', num2str(date_now(3)), num2str(date_now(4)), num2str(date_now(5)));
diary(strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Results_Simulation\','Master_Policy_Design_EEGap_a_standard_marginal_heter_co2_welfare_decomp_demand_grID_',num2str(subsample),'_allinc_hdonly','_',num2str(set_seed),date_now,'.log'));



%Read Files
 	%bchoice=load('H:\Research\sears\estar_data\refrigerators\bchoice_046_2008_2012_v11022017_struct_52171_4.csv'); 
    tmp_file1=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\bchoice_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
 	bchoice=load(tmp_file1); 
    bchoice(:,1)=[];
    MT=size(bchoice,1);
    bchoice(Nsample+1:MT,:)=[];
    bchoice=bchoice';
    [bchoice_i,bchoice_j,bchoice_s]=find(bchoice);
    bchoice_ij=find(bchoice);
    [bchoice_m,bchoice_n] = size(bchoice);
%     clear bchoice;
    
	tmp_file2=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\choiceset_identifier_trimester_week_zipcode_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    id=load(tmp_file2); 
    id(:,1:7)=[];
    id(Nsample+1:MT,:)=[];
    id=id';
    [id_i,id_j,id_s]=find(id);
    id_ij=find(id);
    [id_m,id_n] = size(id);
    
    
    MTvector=ones(MT,1);
    J=size(id,1);
   
    tmp_file3=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\estar_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    estar=load(tmp_file3); 
    estar(:,1:7)=[];
    estar(Nsample+1:MT,:)=[];
    estar=estar';
    estar_num=estar(bchoice_ij);
    estar_denom=estar(id_ij);
    prob_estar_exact=mean(estar,1);
    %clear estar;
    
    tmp_file4=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\rebate_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    rebate=load(tmp_file4); 
    rebate(:,1:7)=[];
    rebate(Nsample+1:MT,:)=[];
    rebate=rebate/1000;
    rebate_J=repmat(rebate,1,J);
    rebate_J=rebate_J';
    rebate_denom=rebate_J(id_ij);
    rebate_estar_num=rebate_J(bchoice_ij).*estar_num;
    rebate_estar_denom=rebate_J(id_ij).*estar_denom;
    rebate_estar_denom=rebate_estar_denom_scale*rebate_estar_denom;
    clear rebate rebate_J;
    
    tmp_file5=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\tax_rate_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    tax=load(tmp_file5); 
    tax(:,1:7)=[];
    tax(Nsample+1:MT,:)=[];
    taxp=1+repmat(tax',J,1);
    
    tmp_file6=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\price_mean_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    price_net=load(tmp_file6); 
    price_net(:,1:7)=[];
    price_net(Nsample+1:MT,:)=[];
    price_net=price_net';
    price=taxp.*price_net;
    price=price/1000;
    price_num=price(bchoice_ij);
    price_denom=price(id_ij);
    clear price_net taxp tax;
    
    tmp_file7=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\kwh_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    kwh=load(tmp_file7); 
    kwh(:,1:7)=[];
    kwh(Nsample+1:MT,:)=[];
    kwh=kwh';
    kwh=kwh/1000;
    %kwh_num=kwh(bchoice_ij);
    %kwh_denom=kwh(id_ij);
    %clear kwh;
    
    tmp_file8=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\elec_county_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    elec=load(tmp_file8); 
    elec(:,1:7)=[];
    elec(Nsample+1:MT,:)=[];
    elec=elec_scale*elec;
	%mean_elec=mean(elec);
	%elec=0*elec+mean_elec;
    %elec_J=repmat(elec,1,J);
    %elec_J=elec_J';
    %elec_num=elec_J(bchoice_ij).*kwh_num;
    %elec_denom=elec_J(id_ij).*kwh_denom;
    %clear elec_J;
    
    tmp_file9=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\demo_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    demo=load(tmp_file9); 

        %hd_id income2 income3 education2 education3 fam_size age political2 political3
    demo(:,1)=[];
    demo(:,1:2)=[];
    demo(Nsample+1:MT,:)=[];
    demo(:,3)=demo(:,3)/10;
    demo(:,4)=demo(:,4)/100;
    demo1_J=repmat(demo(:,1),1,J); %education2
    demo2_J=repmat(demo(:,2),1,J); %education3
    demo3_J=repmat(demo(:,3),1,J); %fam_size
    demo4_J=repmat(demo(:,4),1,J); %age
    demo5_J=repmat(demo(:,5),1,J); %political2
    demo6_J=repmat(demo(:,6),1,J); %political3
   
%Create Interactions Attributes-Demographics     
	tmp_file10=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\type_id_2_3_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');    
	type_id2_3=load(tmp_file10); 
    type_id2_3(:,1:7)=[];
    type_id2_3(Nsample+1:MT,:)=[];
   
    type_id2_3_demo1=type_id2_3.*demo1_J;
    type_id2_3_demo1=type_id2_3_demo1';
    type_id2_3_demo1_num=type_id2_3_demo1(bchoice_ij);
    type_id2_3_demo1_denom=type_id2_3_demo1(id_ij);
    clear type_id2_3_demo1;
    
    type_id2_3_demo2=type_id2_3.*demo2_J;
    type_id2_3_demo2=type_id2_3_demo2';
    type_id2_3_demo2_num=type_id2_3_demo2(bchoice_ij);
    type_id2_3_demo2_denom=type_id2_3_demo2(id_ij);
    clear type_id2_3_demo2;
    
    type_id2_3_demo3=type_id2_3.*demo3_J;
    type_id2_3_demo3=type_id2_3_demo3';
    type_id2_3_demo3_num=type_id2_3_demo3(bchoice_ij);
    type_id2_3_demo3_denom=type_id2_3_demo3(id_ij);
    clear type_id2_3_demo3;
    
    type_id2_3_demo4=type_id2_3.*demo4_J;
    type_id2_3_demo4=type_id2_3_demo4';
    type_id2_3_demo4_num=type_id2_3_demo4(bchoice_ij);
    type_id2_3_demo4_denom=type_id2_3_demo4(id_ij);
    clear type_id2_3_demo4;
    
    type_id2_3_demo5=type_id2_3.*demo5_J;
    type_id2_3_demo5=type_id2_3_demo5';
    type_id2_3_demo5_num=type_id2_3_demo5(bchoice_ij);
    type_id2_3_demo5_denom=type_id2_3_demo5(id_ij);
    clear type_id2_3_demo5;
    
    type_id2_3_demo6=type_id2_3.*demo6_J;
    type_id2_3_demo6=type_id2_3_demo6';
    type_id2_3_demo6_num=type_id2_3_demo6(bchoice_ij);
    type_id2_3_demo6_denom=type_id2_3_demo6(id_ij);
    clear type_id2_3_demo6;
   
    clear type_id2_3;
   
    tmp_file11=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\AV_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');    
    AV=load(tmp_file11); 
    AV(:,1:7)=[];
    AV(Nsample+1:MT,:)=[];
    AV=AV/100;
    
    AV_demo1=AV.*demo1_J;
    AV_demo1=AV_demo1';
    AV_demo1_num=AV_demo1(bchoice_ij);
    AV_demo1_denom=AV_demo1(id_ij);
    clear AV_demo1;
    
    AV_demo2=AV.*demo2_J;
    AV_demo2=AV_demo2';
    AV_demo2_num=AV_demo2(bchoice_ij);
    AV_demo2_denom=AV_demo2(id_ij);
    clear AV_demo2;
    
    AV_demo3=AV.*demo3_J;
    AV_demo3=AV_demo3';
    AV_demo3_num=AV_demo3(bchoice_ij);
    AV_demo3_denom=AV_demo3(id_ij);
    clear AV_demo3;
    
    AV_demo4=AV.*demo4_J;
    AV_demo4=AV_demo4';
    AV_demo4_num=AV_demo4(bchoice_ij);
    AV_demo4_denom=AV_demo4(id_ij);
    clear AV_demo4;
    
    AV_demo5=AV.*demo5_J;
    AV_demo5=AV_demo5';
    AV_demo5_num=AV_demo5(bchoice_ij);
    AV_demo5_denom=AV_demo5(id_ij);
    clear AV_demo5;
    
    AV_demo6=AV.*demo6_J;
    AV_demo6=AV_demo6';
    AV_demo6_num=AV_demo6(bchoice_ij);
    AV_demo6_denom=AV_demo6(id_ij);
    clear AV_demo6;
   
    clear AV;
    
    tmp_file12=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\ice_sc_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');    
    ice_sc=load(tmp_file12); 
    ice_sc(:,1:7)=[];
    ice_sc(Nsample+1:MT,:)=[];
    
    ice_sc_demo1=ice_sc.*demo1_J;
    ice_sc_demo1=ice_sc_demo1';
    ice_sc_demo1_num=ice_sc_demo1(bchoice_ij);
    ice_sc_demo1_denom=ice_sc_demo1(id_ij);
    clear ice_sc_demo1;
    
    ice_sc_demo2=ice_sc.*demo2_J;
    ice_sc_demo2=ice_sc_demo2';
    ice_sc_demo2_num=ice_sc_demo2(bchoice_ij);
    ice_sc_demo2_denom=ice_sc_demo2(id_ij);
    clear ice_sc_demo2;
    
    ice_sc_demo3=ice_sc.*demo3_J;
    ice_sc_demo3=ice_sc_demo3';
    ice_sc_demo3_num=ice_sc_demo3(bchoice_ij);
    ice_sc_demo3_denom=ice_sc_demo3(id_ij);
    clear ice_sc_demo3;
    
    ice_sc_demo4=ice_sc.*demo4_J;
    ice_sc_demo4=ice_sc_demo4';
    ice_sc_demo4_num=ice_sc_demo4(bchoice_ij);
    ice_sc_demo4_denom=ice_sc_demo4(id_ij);
    clear ice_sc_demo4;
    
    ice_sc_demo5=ice_sc.*demo5_J;
    ice_sc_demo5=ice_sc_demo5';
    ice_sc_demo5_num=ice_sc_demo5(bchoice_ij);
    ice_sc_demo5_denom=ice_sc_demo5(id_ij);
    clear ice_sc_demo5;
    
    ice_sc_demo6=ice_sc.*demo6_J;
    ice_sc_demo6=ice_sc_demo6';
    ice_sc_demo6_num=ice_sc_demo6(bchoice_ij);
    ice_sc_demo6_denom=ice_sc_demo6(id_ij);
    clear ice_sc_demo6;
   
    clear ice_sc;
      
    prod=repmat([1:J],Nsample,1)';
    prod_denom=prod(id_ij);
    prod_num=prod(bchoice_ij);
    
    
    tmp_file13=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\estarkwh_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    estarkwh=load(tmp_file13); 
    estarkwh(:,1:7)=[];
    estarkwh(Nsample+1:MT,:)=[];
    estarkwh=estarkwh';
    mefkwh=estarkwh/0.8;
    mefkwh=mefkwh/1000;
    mefkwh_num=mefkwh(bchoice_ij);
    mefkwh_denom=mefkwh(id_ij);
    
     tmp_file13=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\emfac_zip_046_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');    
    em_fac = load(tmp_file13); 
    em_fac(Nsample+1:MT,:)=[];
    %mean_em_fac=mean(em_fac);
    %em_fac=em_fac*0+mean_em_fac;
    d_ext=d_ext_carbon_price*em_fac;
    %d_ext = repmat(d_ext_carbon_price*em_fac',J,1);
    
 clear data;   
  
 
 	tmp_file1=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\choiceset_identifier_trimester_week_zipcode_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
    id=load(tmp_file1); 
	Nmax=size(id,1);
	if subsample == 11000
		bs_max=10*ceil(Nmax/10000);
    elseif subsample == 44000
		bs_max=10*ceil(Nmax/20000); 
    end

c=1
%for j=1:bs_max
for j=1:16
%for j=1:2	
   
	try	
	
	    index_bs=[(1+(j-1)*(1000)):min(j*(1000),Nmax)];
	    tmp_file1=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Data\Matlab_estimation\choiceset_identifier_trimester_week_zipcode_2008_2012_v11022017_struct_',num2str(subsample),'_',num2str(inc),'_seed_',num2str(set_seed),'.csv');
	    id=load(tmp_file1);
	    id=id(index_bs,:);
	    id(:,1:7)=[];
	    choicest_size=sum(id,2);
	    %id_discard=find(choicest_size>200 | choicest_size<50);
	    id_discard=find(choicest_size>400);
	    id(id_discard,:)=[];  
	    choicest_size(id_discard)=[]; 
	    id=id';
	    J_bs=size(id,1);
	
		%results_file=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Results_Estimation/','Fox_discrete_cHD_mixedFEdemoshrt_WTP_v',num2str(subsample),'_inc',num2str(inc),'_sd',num2str(set_seed),'_cty_tau1_grid_v3_','bs_',num2str(j),'.mat');
	    results_file=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Results_Estimation\demand_est_grID\','Fox_discrete_mixedFEdemoshrt_WTP_v',num2str(subsample),'_inc',num2str(inc),'_sd',num2str(set_seed),'_cty_tau1_grid_v3_','bs_',num2str(j),'.mat');
        fox=load(results_file);
	    j 
	    eta_fox_v{c}   = fox.param_v(:,1);
	    theta_m_5_v{c} = fox.param_v(:,2);
        tau_fox_v{c}   = fox.param_v(:,3);
	    weight{c}      = fox.beta;
           
        eta_hom(c)   = eta_fox_v{c}'*weight{c};
        theta_hom(c) = theta_m_5_v{c}'*weight{c};
        
        %Adjustment to rescale m:
        theta_m_2_v    = theta_m_5_v{c}*LLC_5_18/LLC_2_18;
        theta_m_12_v   = theta_m_5_v{c}*LLC_5_18/LLC_12_18;
        
        m_2=LLC_2_18/LLC_5_18;
        m_12=LLC_12_18/LLC_5_18;
        
        id_m_2  = (theta_m_5_v{c} > m_2);
        id_m_12 = (theta_m_5_v{c} < m_12);
        id_neg  = (theta_m_5_v{c}<0);
        
        theta_m_adj = theta_m_5_v{c}*0+1;
        theta_m_adj(id_m_2)  = theta_m_2_v(id_m_2);
        theta_m_adj(id_m_12) = theta_m_12_v(id_m_12);
        theta_m_adj(id_neg)  = theta_m_12_v(id_neg)*0;
        
        theta_fox_v{c}=theta_m_adj;
        
        weight{c}      = fox.beta;
	    matrix_fox_results{c}=[eta_fox_v{c}, theta_fox_v{c}, weight{c}];
	    
% 	    result_homFE=strcat('\\c3\rdat\SHoude\Research\sears\estar_results\struct_est\hom_FEdemoshrt_wtp_LLC_5_18_',num2str(subsample),'_inc',num2str(inc),'_sd',num2str(set_seed),'_04132018_bs_',num2str(j),'.mat');
% 		eval(['load ' result_homFE]);
% 		result_homFE0=theta_est1;
% 
% 		param_FE{c}=result_homFE0(1:J_bs-1,1);
%  		param_FEdemo{c}=result_homFE0(J_bs+4:J_bs+15)
%  
%  		eta(c) = result_homFE0(J_bs);  
%  		tau(c) = result_homFE0(J_bs+1);
%  	    pi(c) = result_homFE0(J_bs+2);
%  		theta(c) = result_homFE0(J_bs+3);
 		
        %result_mixed=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Results_Estimation/','mixed_logit_FEdemoshrt_cHD_wtp_',num2str(subsample),'_',num2str(inc),'_',num2str(set_seed),'_matlab_seed_',num2str(matlab_seed),'_nb_draws_',num2str(nb_draws),'bs_',num2str(j),'.mat');
 		result_mixed=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Results_Estimation\demand_est_grID\','mixed_logit_FEdemoshrt_grID_wtp_100iters_',num2str(subsample),'_',num2str(inc),'_',num2str(set_seed),'_matlab_seed_',num2str(matlab_seed),'_nb_draws_',num2str(nb_draws),'bs_',num2str(j),'.mat');
        eval(['load ' result_mixed]);
		result_mixed0=theta_est1;
		param_FE{c}=result_mixed0(1:J_bs-1,1);
		param_FEdemo{c}=result_mixed0(J_bs+4:J_bs+15);
		
  		tau(c)=result_mixed0(J_bs+1);
		pi(c)=result_mixed0(J_bs+2);

		id_eta_neg{c} = find(matrix_fox_results{c}(:,1)<=0);
		eta_restricted{c}=matrix_fox_results{c}(id_eta_neg{c},:);
		eta_mean_experience(c)=eta_restricted{c}(:,1)'*eta_restricted{c}(:,3);
				
       eta_hom(c)= matrix_fox_results{c}(:,1)'*matrix_fox_results{c}(:,3);
       theta_hom(c)= matrix_fox_results{c}(:,2)'*matrix_fox_results{c}(:,3);
       
 		id_weight{c} = find(matrix_fox_results{c}(:,3)>=5e-4);
 	    results_k{c}=matrix_fox_results{c}(id_weight{c},:);
 		dim_k{c}=size(results_k{c},1);
 
       eta_theta_k=[];
		 for k=1:dim_k{c}
		   if results_k{c}(k,1)>0
		     eta_experience(c) = eta_mean_experience(c); 
		   else
		     eta_experience(c) = results_k{c}(k,1); 
           end    
		     eta_theta_k{k}=[results_k{c}(k,1),eta_experience(c),results_k{c}(k,2),tau(c),pi(c)];
         end
        weight_c=results_k{c}(:,3);

		%% 
		% Step 1: For each bs iteration we get the change in externality costs for a given d_ext_carbon_price
        carbon_price = d_ext_carbon_price.*em_fac;
		t_pigou_it   = carbon_price;

        [D_CS_tax, D_Ext_tax, D_Mis1_tax, D_Mis2_tax, D_Mis_tax, D_SW_tax, m_theta_tax, m_D_E_tax, m_E_tax, sd_theta_tax, sd_D_E_tax, sd_E_tax, corr_theta_D_E_tax, corr_theta_E_tax, D_Mis_SumStats_tax, D_Mis_SumStats_1_tax, D_Mis_SumStats_2_tax, D_Mis_SumStats_3_tax, D_Mis_SumStats_4_tax] = ...
    Total_Welfare_decomp_WTP_EEgap_tax_heter(weight_c,eta_theta_k,param_FE{c},param_FEdemo{c},id_ij,J,p_elec,t_pigou_it,d_ext,elec,lifetime,r,MCPF,Nsample,price, price_denom, rebate_estar_denom, estar_denom, kwh, prod_denom,id_i, id_j, id_m, id_n, type_id2_3_demo1_denom, AV_demo1_denom,  ice_sc_demo1_denom, type_id2_3_demo2_denom,   AV_demo2_denom,  ice_sc_demo2_denom,type_id2_3_demo3_denom,   AV_demo3_denom,  ice_sc_demo3_denom,type_id2_3_demo4_denom,   AV_demo4_denom,  ice_sc_demo4_denom,type_id2_3_demo5_denom,  AV_demo5_denom,  ice_sc_demo5_denom, type_id2_3_demo6_denom,  AV_demo6_denom,  ice_sc_demo6_denom);

        %%
        % Step 2: We simulate a change in attribute standard for a several
        % values
		kwh_denom=kwh(id_ij);  
		cost_base_denom=(191/1000)./kwh_denom;		
        
		for i=1:421
         
         t_pigou=0;
		 pct_standard(i)=((i+279)/7)/100;
		 kwh_min=mefkwh*(((i+279)/7)/100);
		 kwh_standard=min(kwh_min,kwh);
		 kwh_standard_denom=kwh_standard(id_ij);
        
		 cost_standard_denom=(191/1000)./kwh_standard_denom;
		 diff_price_denom=cost_standard_denom-cost_base_denom;
		 price_standard_denom=price_denom+diff_price_denom;
		  
		 [D_CS(i), D_Ext(i), D_Mis_marginal(i), D_Mis_infra(i), D_Mis(i), D_SW(i), m_theta(i), m_D_E(i), m_D_E_marginal(i), m_D_E_infra(i), sd_theta(i), sd_D_E(i), sd_D_E_marginal(i), sd_D_E_infra(i), corr_theta_D_E(i), corr_theta_D_E_marginal(i), corr_theta_D_E_infra(i), D_Mis_SumStats(i), D_Mis_SumStats_1(i), D_Mis_SumStats_2(i), D_Mis_SumStats_1_marginal(i), D_Mis_SumStats_2_marginal(i),D_Mis_SumStats_1_infra(i), D_Mis_SumStats_2_infra(i)] = ...
    Total_Welfare_decomp_WTP_EEgap_standard_marginal(weight_c,eta_theta_k,param_FE{c},param_FEdemo{c},id_ij,J,p_elec,t_pigou,d_ext,elec,lifetime,r,MCPF,Nsample, price, price_denom, price_standard_denom, rebate_estar_denom, estar_denom, kwh, kwh_standard, prod_denom,id_i, id_j, id_m, id_n, type_id2_3_demo1_denom, AV_demo1_denom,  ice_sc_demo1_denom, type_id2_3_demo2_denom,   AV_demo2_denom,  ice_sc_demo2_denom,type_id2_3_demo3_denom,   AV_demo3_denom,  ice_sc_demo3_denom,type_id2_3_demo4_denom,   AV_demo4_denom,  ice_sc_demo4_denom,type_id2_3_demo5_denom,  AV_demo5_denom,  ice_sc_demo5_denom, type_id2_3_demo6_denom,  AV_demo6_denom,  ice_sc_demo6_denom);
  
         i
		end
		
        %%
        % Step 3: 
        %We look for the run that matches the change in externality
        %obtained for the tax and that is an input for this script. 
		%match_tax_id_high         = find(D_Ext > D_Ext_tax);
        %match_tax_id              = max(1,min(match_tax_id_high)-1);
		%pct_standard_star(c)      = pct_standard(match_tax_id ); 
        match_tax_id_high         = find(round(10*(D_Ext - D_Ext_tax))==1);
        match_tax_id              = max(1,min(match_tax_id_high(1)));
		pct_standard_star(c)      = pct_standard(match_tax_id ); 
		kwh_min_star              = mefkwh*pct_standard_star(c);
		kwh_standard_star         = min(kwh_min_star,kwh);
		kwh_standard_star_denom   = kwh_standard_star(id_ij);
		
		cost_standard_star_denom  = (191/1000)./kwh_standard_star_denom;
		diff_price_star_denom     = cost_standard_star_denom-cost_base_denom;
		price_standard_star_denom = price_denom+diff_price_star_denom;

		D_SW_bs{inc,c}                      = D_SW(match_tax_id);
		D_CS_bs{inc,c}                      = D_CS(match_tax_id);
		D_Mis_bs{inc,c}                     = D_Mis(match_tax_id);
        D_Mis_marginal_bs{inc,c}            = D_Mis_marginal(match_tax_id);
        D_Mis_infra_bs{inc,c}               = D_Mis_infra(match_tax_id);
		D_Ext_bs{inc,c}                     = D_Ext(match_tax_id);
		D_Mis_SumStats_1_marginal_bs{inc,c} = D_Mis_SumStats_1_marginal(match_tax_id); 
        D_Mis_SumStats_2_marginal_bs{inc,c} = D_Mis_SumStats_2_marginal(match_tax_id);
        D_Mis_SumStats_1_infra_bs{inc,c}    = D_Mis_SumStats_1_infra(match_tax_id); 
        D_Mis_SumStats_2_infra_bs{inc,c}    = D_Mis_SumStats_2_infra(match_tax_id);
        m_theta_bs{inc,c}                   = m_theta(match_tax_id);
        m_D_E_bs{inc,c}                     = m_D_E(match_tax_id);
        sd_theta_bs{inc,c}                  = sd_theta(match_tax_id);
        sd_D_E_bs{inc,c}                    = sd_D_E(match_tax_id);
        corr_theta_D_E_bs{inc,c}            = corr_theta_D_E(match_tax_id);
        corr_theta_D_E_marginal_bs{inc,c}   = corr_theta_D_E_marginal(match_tax_id);
        corr_theta_D_E_infra_bs{inc,c}      = corr_theta_D_E_infra(match_tax_id);

	    c=c+1;
	catch
		c=c;	
	end
    
end

Result_t_Welfare_Decomp      = full([pct_standard_star', cell2mat(D_SW_bs)', cell2mat(D_CS_bs)', cell2mat(D_Ext_bs)', cell2mat(D_Mis_bs)', cell2mat(D_Mis_marginal_bs)', cell2mat(D_Mis_infra_bs)']); 
Result_t_Welfare_Decomp_mean = sum(Result_t_Welfare_Decomp,1)/(c-1);
Result_t_Welfare_Decomp_se   = std(Result_t_Welfare_Decomp,1)/((c-1)^.5);

Result_t_Sufficient_Stats      = full([cell2mat(m_theta_bs)',  cell2mat(m_D_E_bs)', cell2mat(sd_theta_bs)', cell2mat(sd_D_E_bs)', cell2mat(corr_theta_D_E_bs)', cell2mat(corr_theta_D_E_marginal_bs)', cell2mat(corr_theta_D_E_infra_bs)', cell2mat(D_Mis_SumStats_1_marginal_bs)', cell2mat(D_Mis_SumStats_2_marginal_bs)', cell2mat(D_Mis_SumStats_1_infra_bs)', cell2mat(D_Mis_SumStats_2_infra_bs)']);   
Result_t_Sufficient_Stats_mean = sum(Result_t_Sufficient_Stats,1)/(c-1);
Result_t_Sufficient_Stats_se   = std(Result_t_Sufficient_Stats,1)/((c-1)^.5);

                              
results_file2=strcat('Z:\FAC\HEC\DEEP\shoude\default\D2c\eegap\EEgap_data_code_heter_SM\Results_Simulation\','Tables_Welfare_Decomp_a_standard_marginal_Heterinc_Heter_pelec_co2_m_adj_pos_', num2str(subsample),'_inc_',num2str(inc), '_sd', num2str(set_seed),'_d_ext_carbon_price_', num2str(d_ext_carbon_price), '_FOX_bs.mat');    
eval(['save ' results_file2 '  Result_t_Welfare_Decomp Result_t_Welfare_Decomp_mean Result_t_Welfare_Decomp_se Result_t_Sufficient_Stats Result_t_Sufficient_Stats_mean Result_t_Sufficient_Stats_se']);  
		

end 






      





